﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BLL;

namespace ChamCongMain.Form
{
    public partial class frmDatHang : DevExpress.XtraEditors.XtraForm
    {
        ChucNang cn;
        public Delegates.RefeshGrid LoadData;
        long id = 0;

        public frmDatHang(ChucNang cn, DatHang dh)
        {
            InitializeComponent();
            LoadDataToGridLookup();
            this.cn = cn;
            dateNgayDat.EditValue = DateTime.Today;
            dateNgayHetHan.EditValue = DateTime.Today.AddMonths(1);
            calSoLuong.EditValue = 0;
            if (this.cn == ChucNang.Them)
            {
                this.Text = "Thêm Đặt Hàng";
            }
            else
            {
                this.Text = "Sửa Đặt Hàng";
                SetValueForForm(dh);
            }
        }

        private void LoadDataToGridLookup()
        {
            SanPhamBLL spbll = new SanPhamBLL();
            DonViBLL dv = new DonViBLL();
            glkSanPham.Properties.ValueMember = "SanPhamId";
            glkSanPham.Properties.DisplayMember = "TenSanPham";
            glkSanPham.Properties.DataSource = spbll.GetAllSanPham();
            gluDonVi.Properties.DataSource = dv.GetAllDonVi();
        }

        private void SetValueForForm(DatHang dh)
        {
            id = dh.DatHangId;
            txtTenDonHang.Text = dh.TenDonHang;
            txtCMND.Text = dh.CMNDKhachHang;
            txtDiaChi.Text = dh.DiaChiLienHe;
            txtGhiChu.Text = dh.GhiChu;
            txtSDT.Text = dh.SdtKhachHang;
            txtTenKhachHang.Text = dh.TenKhachHang;
            dateNgayDat.EditValue = dh.NgayDat;
            dateNgayHetHan.EditValue = dh.NgayHetHan;
            if (dh.SanPhamId!=null)
            glkSanPham.EditValue = dh.SanPhamId;
            calSoLuong.EditValue = dh.SoLuongSanPham;
            if (dh.MaDonVi != null)
                gluDonVi.EditValue = dh.MaDonVi;
        }

        private DatHang GetValueFromForm()
        {
            DatHang dh = new DatHang();
            dh.DatHangId = id;
            dh.TenDonHang = txtTenDonHang.Text;
            if(glkSanPham.EditValue!=null)
                dh.SanPhamId = (long)glkSanPham.EditValue;
            dh.TenKhachHang = txtTenKhachHang.Text;
            if(calSoLuong.EditValue!=null)
                dh.SoLuongSanPham = int.Parse(calSoLuong.EditValue.ToString());
            if (gluDonVi.EditValue != null)
                dh.MaDonVi = (long)gluDonVi.EditValue;
            dh.SdtKhachHang = txtSDT.Text;
            dh.NgayDat = (DateTime)dateNgayDat.EditValue;
            dh.NgayHetHan = (DateTime)dateNgayHetHan.EditValue;
            dh.DiaChiLienHe = txtDiaChi.Text;
            dh.CMNDKhachHang = txtCMND.Text;
            dh.GhiChu = txtGhiChu.Text;
            return dh;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            DatHang dh = GetValueFromForm();
            DatHangBLL dhbll = new DatHangBLL();
            if (cn == ChucNang.Them)
            {
                dhbll.InsertDatHang(dh);
                LoadData(dhbll.GetAllDatHang());
                XtraMessageBox.Show("Thêm thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dhbll.UpdateDatHang(dh);
                LoadData(dhbll.GetAllDatHang());
                XtraMessageBox.Show("Sửa thành công!", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            this.Close();
        }

        private void btnLuuThem_Click(object sender, EventArgs e)
        {
            DatHang dh = GetValueFromForm();
            DatHangBLL dhbll = new DatHangBLL();
            if (cn == ChucNang.Them)
            {
                dhbll.InsertDatHang(dh);
                LoadData(dhbll.GetAllDatHang());
                XtraMessageBox.Show("Thêm thành công!", "Thêm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                dhbll.UpdateDatHang(dh);
                LoadData(dhbll.GetAllDatHang());
                XtraMessageBox.Show("Sửa thành công!", "Sửa", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            ClearForm();
        }

        private void ClearForm()
        {
            txtGhiChu.Text = string.Empty;
            txtTenDonHang.Text = string.Empty;
            txtDiaChi.Text = string.Empty;
            txtCMND.Text = string.Empty;
            txtSDT.Text = string.Empty;
            txtTenKhachHang.Text = string.Empty;
            calSoLuong.EditValue = 0;
            dateNgayDat.EditValue = DateTime.Today;
            dateNgayHetHan.EditValue = DateTime.Today.AddMonths(1);
            glkSanPham.EditValue = null;
            gluDonVi.EditValue = null;
            cn = ChucNang.Them;
            this.Text = "Thêm Đặt Hàng";
        }

        private void btnHuyBo_Click(object sender, EventArgs e)
        {
            DialogResult re = XtraMessageBox.Show("Bạn có muốn thoát không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (re == DialogResult.Yes)
                this.Close();
        }

    }
}